/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.univnantes.alma.arbres.affichage;

import fr.univnantes.alma.arbres.Arbre;
import fr.univnantes.alma.arbres.Arbre.ArbreIteratorType;
import fr.univnantes.alma.arbres.Noeud;
import java.util.Iterator;

/**
 *
 * @author judu
 */
public class AffichageNoeudsArcs<T> {

    private Arbre<T> arbre;
    private final ArbreIteratorType typeIt;

    public AffichageNoeudsArcs(Arbre<T> arbre, Arbre.ArbreIteratorType typeIt) {
        this.typeIt = typeIt;
        this.arbre = arbre;
    }

    @Override
    public String toString() {
        return this.print();
    }

    private String print() {
        Iterator<Noeud<T>> it = this.arbre.iterator(this.typeIt);
        String noeuds = new String("{");
        String arcs = new String("{");

        while(it.hasNext()){
            Noeud<T> current = it.next();

            noeuds = noeuds.concat(current.toString());

            if(it.hasNext()){
                noeuds = noeuds.concat(", ");
            } else {
                noeuds = noeuds.concat("}\n");
            }

            String arcstemp = "";
            if(this.arbre.hasFils(current)){

                Noeud<T> fils = this.arbre.getFirstFils(current);
                
                while(this.arbre.hasNextFrere(fils)){

                    arcstemp = arcstemp.concat("("+current.getEtiquette()+", "+fils.getEtiquette()+"), ");
                    fils = this.arbre.getNextFrere(fils);
                }
                arcstemp = arcstemp.concat("("+current.getEtiquette()+", "+fils.getEtiquette()+")");
            }

            if(!arcstemp.isEmpty()){
                if(arcs.equals("{")){
                    arcs = arcs.concat(arcstemp);
                } else {
                    arcs = arcs.concat(", "+arcstemp);
                }
                
            }
            
        }
        arcs = arcs.concat("}");
        


        return noeuds.concat(arcs);
    }



}
